package project.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import project.pojo.LoginClass;
import project.pojo.Student;
import project.pojo.Teacher;
import project.service.StudentService;
import project.service.TeacherService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private StudentService studentService = new StudentService();
    private TeacherService teacherService = new TeacherService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();

        LoginClass user = JSON.parseObject(s, LoginClass.class);
        if(user.status == 0){
            Student student = studentService.judgeLogin(user.getId(), user.getPassword());
            if(student == null){
                response.getWriter().write("unsuccess");
            }else{
                response.getWriter().write("success");
            }
        }else{
            Teacher teacher = teacherService.judgeLogin(user.getId(), user.getPassword());
            if(teacher == null){
                response.getWriter().write("unsuccess");
            }else{
                response.getWriter().write("success");
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
